package ${package_name}.service;

import com.chdz.common.dynamic.Criteria;
import com.chdz.common.mysql.entity.${model_name};
import com.chdz.common.mysql.repository.${model_name}Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;
import com.chdz.common.response.CreatedResponse;

/**
* 描述：${table_annotation}服务层
* @author ${author}
* @date ${date}
*/
@Service
public class ${model_name}Service {

    @Autowired
    private ${model_name}Repository ${model_name?uncap_first}Repository;
    /**
     * 功能描述:分页查询
     * @param:/**请自定义查询参数*/
     * @return:
     * @auther: ${author}
     * @date: ${date}
     */
    public ResponseEntity<?> get${model_name}s(Pageable pageable,HttpServletRequest request) throws Exception{
        BoolQueryBuilder queryBuilder= QueryBuilders.boolQuery();
        <#--if(!StringUtils.isEmpty("字段名")) {-->
            <#--queryBuilder.must(new QueryStringQueryBuilder("参数").field("字段名"));-->
        <#--}-->
        <#--if(!StringUtils.isEmpty("参数")){-->
            <#--BoolQueryBuilder subQueryBuilder= QueryBuilders.boolQuery();-->
            <#--subQueryBuilder.should(new WildcardQueryBuilder("字段名","*参数*"));-->
            <#--subQueryBuilder.should(new WildcardQueryBuilder("字段名","*参数*"));-->
            <#--queryBuilder.must(subQueryBuilder);-->
        <#--}-->
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        if(queryBuilder.hasClauses()){
            nativeSearchQueryBuilder.withQuery(queryBuilder);
        }
        nativeSearchQueryBuilder.withPageable(pageable);
        SearchQuery searchQuery =nativeSearchQueryBuilder.build();
        Page<${model_name}> page = esAlarmEventRepository.search(searchQuery);
		
		if (null == page || CollectionUtils.isEmpty(page.getContent())) {
			throw new SysRuntimeException("请输入自定义异常CODE", HttpStatus.NO_CONTENT);
		}

        List<${model_name}Response> contents = new ArrayList();
        if(page.getContent() != null && page.getContent().size() > 0){
            for(${model_name} ${model_name?uncap_first} : page.getContent()){
                ${model_name}Response ${model_name?uncap_first}Response = new ${model_name}Response();
                BeanUtils.copyPropertiesIgnoreNullValue(${model_name?uncap_first}, ${model_name?uncap_first}Response);
                contents.add(${model_name?uncap_first}Response);
            }
        }
        List<Link> list = LinkUtils.prepareLinks(pageable.getPageNumber(),
        pageable.getPageSize(), request, page,"");
        PagedResources<${model_name}Response> pagedResources = new PagedResources<${model_name}Response>(
            contents, new PagedResources.PageMetadata(page.getSize(), page.getNumber(),
            page.getTotalElements(), page.getTotalPages()),list);
        return new ResponseEntity<PagedResources<${model_name}Response>>(pagedResources, HttpStatus.OK);
    }

    /**
    * 功能描述:
    * @param:
    * @return:
    * @auther: ${author}
    * @date: ${date}
    */
    public ResponseEntity<?> get${model_name}ById(String uuid) throws Exception{
        ${model_name} one = ${model_name?uncap_first}Repository.findOne(uuid);
        if(one == null){
            throw new SysRuntimeException("请输入自定义异常CODE", HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<${model_name}>(one,HttpStatus.OK);
    }
    /**
    * 功能描述:
    * @param:
    * @return:
    * @auther: ${author}
    * @date: ${date}
    */
    public ResponseEntity<?> add${model_name}(${model_name} ${model_name?uncap_first})throws Exception{
        ${model_name?uncap_first}.setId(UUID.randomUUID().toString());
        ${model_name?uncap_first}Repository.save(${model_name?uncap_first});
        
        CreatedResponse response = new CreatedResponse(new${model_name}.getId());
		return new ResponseEntity<CreatedResponse>(response, HttpStatus.CREATED);
    }
    /**
    * 功能描述:
    * @param:
    * @return:
    * @auther: ${author}
    * @date: 2018/07/24
    */
    public ResponseEntity<?> update${model_name}(String uuid,${model_name} ${model_name?uncap_first})throws Exception{
        ${model_name} one = ${model_name?uncap_first}Repository.findOne(uuid);
        if (one == null) {
            throw new SysRuntimeException("请输入自定义异常CODE", HttpStatus.NOT_FOUND);
        }
        BeanUtils.copyPropertiesIgnoreNullValue(updateAlarmEvent, one);
        one.setUpdateTime(new Date().getTime());
        one.setId(uuid);
        ${model_name} save = ${model_name?uncap_first}Repository.save(one);
        return new ResponseEntity<HttpStatus>(HttpStatus.OK);
    }
    /**
    * 功能描述:
    * @param:
    * @return:
    * @auther: ${author}
    * @date: ${date}
    */
    public ResponseEntity<?> delete${model_name}(String uuid) throws Exception{
   	     ${model_name} one = ${model_name?uncap_first}Repository.findOne(uuid);
        if(one == null){
            throw new SysRuntimeException("请输入自定义异常CODE", HttpStatus.NOT_FOUND);
        }
        ${table_name?uncap_first}Repository.delete(id);
        return new ResponseEntity<HttpStatus>(HttpStatus.OK);
    }

}